<?php

function getTestChart() {
    echo "<html>
<head>
<!--Load the AJAX API-->
<script type = \"text/javascript\" src = \"https://www.google.com/jsapi\"></script>
    <script type = \"text/javascript\">

// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {

// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Task 1', 4],
 ['Task 2', 2],
 ['Task 3', 1],
]);

// Set chart options
var options = {'title':'Project 1',
 'width':400,
 'height':300};

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>

<body>
    <!--Div that will hold the pie chart-->
    <div id=\"chart_div\"></div>
</body>
</html>";
}

function getTimelineChartScript() {

    echo "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization',
       'version':'1','packages':['timeline']}]}\"></script>
<script type=\"text/javascript\">
google.setOnLoadCallback(drawChart);

function drawChart() {
  var container = document.getElementById('example1');

  var chart = new google.visualization.Timeline(container);

  var dataTable = new google.visualization.DataTable();

  dataTable.addColumn({ type: 'string', id: 'President' });
  dataTable.addColumn({ type: 'date', id: 'Start' });
  dataTable.addColumn({ type: 'date', id: 'End' });

  dataTable.addRows([
    [ 'Washington', new Date(1789, 3, 29), new Date(1797, 2, 3) ],
    [ 'Adams',      new Date(1797, 2, 3),  new Date(1801, 2, 3) ],
    [ 'Jefferson',  new Date(1801, 2, 3),  new Date(1809, 2, 3) ]]);

  chart.draw(dataTable);
}
</script>";
}

function getTimelineChartScriptFromArray($array, $name) {
    echo "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization', 'version':'1','packages':['timeline']}]}\"></script>
          <script type=\"text/javascript\">
            google.setOnLoadCallback(drawChart);

            function drawChart() {
             var container = document.getElementById('$name');
             var chart = new google.visualization.Timeline(container);
             var dataTable = new google.visualization.DataTable();

             dataTable.addColumn({ type: 'string', id: 'Tasks' });
             dataTable.addColumn({ type: 'string', id: 'Status' });
             dataTable.addColumn({ type: 'date', id: 'Start' });
             dataTable.addColumn({ type: 'date', id: 'End' });

             dataTable.addRows([";
                for ($i = 0; $i < sizeof($array); $i++) {
                   if ($i === 0) {
                      echo $array[$i];
                   } else {
                      echo ',' . $array[$i];
                   }
                }
            echo "]);
           chart.draw(dataTable);
           }
       </script>";
}

function getTimelineChartScript2($result, $name) {

    echo 'numRows: ' . $result->num_rows . '<br>';
    $rows = array();
    while ($row = mysqli_fetch_array($result)) {
        $taskId = $row['taskID'];
        $startDate = $row['startDate'];
        $startVals = explode("-", $startDate);
        $endDate = $row ['endDate'];
        $endVals = explode("-", $endDate);
        $singleRow = "[ 'Task $taskId', '% Complete', new Date($startVals[0], $startVals[1], $startVals[2]), new Date($endVals[0], $endVals[1], $endVals[2]) ]";
        array_push($rows, $singleRow);
    }

    echo "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization',
       'version':'1','packages':['timeline']}]}\"></script>
<script type=\"text/javascript\">
google.setOnLoadCallback(drawChart);

function drawChart() {
  var container = document.getElementById('$name');

  var chart = new google.visualization.Timeline(container);

  var dataTable = new google.visualization.DataTable();

  dataTable.addColumn({ type: 'string', id: 'Tasks' });
  dataTable.addColumn({ type: 'string', id: 'Status' });
  dataTable.addColumn({ type: 'date', id: 'Start' });
  dataTable.addColumn({ type: 'date', id: 'End' });

  dataTable.addRows([";

    for ($i = 0; $i < sizeof($rows); $i++) {
        if ($i === 0) {
            echo $rows[$i];
        } else {
            echo ',' . $rows[$i];
        }
    }

    echo "]);
  chart.draw(dataTable);
}
</script>";
}

function getTimelineChartScrip($result) {

    echo "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}\"></script>";
    echo "<script type=\"text/javascript\">";
    echo "google.setOnLoadCallback(drawChart);";

    echo "function drawChart() {
             var container = document.getElementById('example1');
             var chart = new google.visualization.Timeline(container);
             var dataTable = new google.visualization.DataTable();";

    echo "dataTable.addColumn({ type: 'string', id: 'Task' });
           dataTable.addColumn({ type: 'date', id: 'Start' });
           dataTable.addColumn({ type: 'date', id: 'End' });";

    $rows = array();
    do {
        $taskId = $row['taskId'];
        $startDate = $row['startDate'];
        $startVals = explode("-", $startDate);
        $endDate = $row ['endDate'];
        $endVals = explode("-", $endDate);
        $singleRow = "[ 'Task $taskId, new Date($startVals[0], $startVals[1], $startVals[2]]), new Date($endVals[0], $endVals[1], $endVals[2]) ]";
        array_push($rows, $singleRow);
    } while ($row = mysqli_fetch_array($result));

    echo "dataTable.addRows([";
    for ($i = 0; $i < sizeof($rows); $i++) {
        if ($i === 0) {
            echo $rows[$i];
        } else {
            echo ',' . $rows[$i];
        }
    }
    echo "]);";
    echo "chart . draw(dataTable);
         }
         </script>";
}

function getTimelineDiv($name, $height) {
    $height = $height.'px';
    echo "<div id='$name'  style=\"width: 1000px; height: $height;\"></div>";
}

?>
